{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from selenium import webdriver\n",
    "\n",
    "driver = webdriver.Chrome()\n",
    "url = 'https://www.csdn.net/'\n",
    "driver.get(url)\n",
    "driver.maximize_window()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "任务完成\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import requests as rq\n",
    "from splinter import Browser\n",
    "\n",
    "# BASE_URL = 'https://mp.weixin.qq.com/s?__biz=MzU2OTI3NjQ4OA==&mid=100000538&idx=1&sn=419a647c0372d7630a197db9454bddc6&chksm=7c80623d4bf7eb2bca0756d63ee7250807d225a1fffe475db317d7c745b51074e5cc58b3e607&mpshare=1&scene=2&srcid=11076nyob9dLyKeQrFDgcnwY&sharer_sharetime=1565615878241&sharer_shareid=80ca6d7f86bae39e4b8264e1365d4b6a&from=timeline&clicktime=1565621528&key=0053416997e154650cb74184219ffa63550a1b3adf6c5c6bc705442434f0b0d05fbbe8217ed41a8f0f9165a2008bacbba44b624f535047b02cff8b47d698619ce97e48cf5663091b25deb3276764083a3719066790537f1813f0d7e3d5b6ae32fcc8cc3e49633034326feda342a8772d88981eedb113ea9a491c345c07ab271e&ascene=1&uin=MTY3NjYy&devicetype=Windows+10+x64&version=62090529&lang=zh_CN&exportkey=AaZhp%2BDCkmag01n7EMTREmg%3D&pass_ticket=hbFyRKZiXYK0ETPppZH0Xbwm2Tnpv7pFXeyOGE846fyLoLRcyDdPwaWI4mjwJzSu&wx_header=0'\n",
    "BASE_URL = 'https://mp.weixin.qq.com/s?t=pages/audio_detail&scene=1&__biz=MzU5OTgyNTEyMA==&mid=2247495390&idx=2&sn=4c5fe1d66fb6f3b824da0c0af231b31c&voice_id=MzU5OTgyNTEyMF8yMjQ3NDk1Mzg5&_wxindex_=0&exportkey=Acnzi6%2FPmF7A9m6if53CBgw%3D&pass_ticket=EupOW2b2N%2FQAdI8RqgZhUNdc2PXFglld70rst1%2BzlGXQ%2BYXfYNEbHUTWyarBe06D&wx_header=0'\n",
    "\n",
    "bro = Browser('chrome')\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    # 访问目标页面\n",
    "    bro.visit(BASE_URL)\n",
    "    # 获取所有列表DOM\n",
    "    links = bro.find_by_css('a[data-linktype=\"2\"]')\n",
    "    # 由于后面需要进行页面跳转，所以先将列表信息解析后存到数组变量中\n",
    "    tasks = []\n",
    "    for link in links:\n",
    "        if link.text:\n",
    "            tasks.append({\n",
    "                'title': link.text,\n",
    "                'url': link['href']\n",
    "            })\n",
    "    # 开始遍历任务\n",
    "    for i, t in enumerate(tasks):\n",
    "        filename = t['title']\n",
    "        filepath = os.path.join(os.getcwd(), 'mp3', f'{filename}.mp3')\n",
    "\n",
    "        print(f'正在下载\\t{i}/{len(tasks)}\\t{filename}')\n",
    "\n",
    "        bro.visit(t['url'])\n",
    "        # 微信公众号页面中的音频文件只有在点击了开始播放后才会在HTML中加载出来<audio>标签\n",
    "        bro.find_by_css('.audio_card_switch').click()\n",
    "        audio_src = bro.find_by_tag('audio')['src']\n",
    "        # 用requests直接下载到文件\n",
    "        open(filepath, 'wb').write(rq.get(audio_src).content)\n",
    "        \n",
    "    bro.quit()\n",
    "    print('任务完成')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.9"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
